package com.kakaxi.mybilibili.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.base.BaseRecyAdapter;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.view.GridMarginDecoration;
import com.kakaxi.mybilibili.bean.DramaRecommendItemBean;
import com.kakaxi.mybilibili.bean.DramaTotalItemBean;
import com.kakaxi.mybilibili.utils.SliderLayoutHelper;
import com.kakaxi.mybilibili.viewholder.BannerViewHolder;
import com.kakaxi.mybilibili.viewholder.DramaRecommendViewHolder;
import com.kakaxi.mybilibili.viewholder.DramaThreeBtnViewHolder;
import com.kakaxi.mybilibili.viewholder.DramaTitleViewHolder;
import com.kakaxi.mybilibili.viewholder.SerializingViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 番剧主页面适配器
 * Created by kakaxi on 16/10/12.
 */

public class DramaHomeRecyAdapter extends BaseRecyAdapter<RecyclerView.ViewHolder> {

    /** 数据总集 **/
    private DramaTotalItemBean mDramaTotalItemBean;

    /** 轮播类型 **/
    private static final int TYPE_BANNER = 0;
    /** 三个按钮类型 **/
    private static final int TYPE_THREE_BUTTON = 1;
    /** 新番标题类型 **/
    private static final int TYPE_NEW_TITLE = 2;
    /** 新番内容类型 **/
    private static final int TYPE_NEW_CONTENT = 3;
    /** 上一季新番标题类型 **/
    private static final int TYPE_LAST_TITLE = 4;
    /** 上一季度新番内容类型 **/
    private static final int TYPE_LAST_CONTENT = 5;
    /** 推荐番剧标题类型 **/
    private static final int TYPE_RECOMMEND_TITLE = 6;
    /** 推荐番剧内容类型 **/
    private static final int TYPE_RECOMMEND_CONTENT = 7;

    /** 监听器 **/
    private OnItemClickListener mListener;

    public DramaHomeRecyAdapter(Context context, DramaTotalItemBean mDramaTotalItemBean) {
        super(context);
        this.mDramaTotalItemBean = mDramaTotalItemBean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        // LogUtil.d("我是创建viewholoder的人" + viewType);
        switch (viewType) {
            case TYPE_BANNER: // 轮播
                viewHolder = new BannerViewHolder(quickInflate(R.layout
                        .item_recy_common_banner, parent));
                break;
            case TYPE_THREE_BUTTON: // 三个按钮
                // LogUtil.d("创建三个按钮");
                viewHolder = new DramaThreeBtnViewHolder(quickInflate(R.layout
                        .item_recy_drama_three_btn, parent));
                break;
            case TYPE_NEW_TITLE: // 新番连载标题
            case TYPE_LAST_TITLE: // 上一季度连载标题
            case TYPE_RECOMMEND_TITLE: // 推荐番剧标题
                viewHolder = new DramaTitleViewHolder(quickInflate(R.layout
                        .item_recy_drama_title, parent));
                break;
            case TYPE_NEW_CONTENT: // 新番连载内容
                viewHolder = new SerializingViewHolder(quickInflate(R.layout
                        .item_recy_drama_new_content, parent), true);
                break;
            case TYPE_LAST_CONTENT:  // 上一季度连载内容
                viewHolder = new SerializingViewHolder(quickInflate(R.layout
                        .item_recy_drama_last_content, parent), false);
                break;
            case TYPE_RECOMMEND_CONTENT: // 推荐番剧内容
                viewHolder = new DramaRecommendViewHolder(quickInflate(R.layout
                        .item_recy_drama_recommend_content, parent));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_BANNER: // 轮播
                final BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
                final List<BannerItemBean> bannerItemBeanList = mDramaTotalItemBean
                        .getBannerItemList();
                final SliderLayout sliderLayout = bannerViewHolder.bannerSliLayout;
                // 设置数据和监听事件
                SliderLayoutHelper.setUrlSlider(sliderLayout, bannerItemBeanList, new
                        BaseSliderView.OnSliderClickListener() {
                            @Override
                            public void onSliderClick(BaseSliderView slider) {
                                if (mListener != null)
                                    mListener.onBannerClick(sliderLayout, bannerItemBeanList);
                            }
                        });
                break;
            case TYPE_THREE_BUTTON: // 三个按钮
                DramaThreeBtnViewHolder threeBtnViewHolder = (DramaThreeBtnViewHolder) holder;
                threeBtnViewHolder.firstBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null)
                            mListener.onThreeBtnClick(v, 0);
                    }
                });
                threeBtnViewHolder.secondBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null)
                            mListener.onThreeBtnClick(v, 1);
                    }
                });
                threeBtnViewHolder.thirdBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null)
                            mListener.onThreeBtnClick(v, 2);
                    }
                });
                break;
            case TYPE_NEW_TITLE: // 新番连载标题
                DramaTitleViewHolder titleViewHolder = (DramaTitleViewHolder) holder;
                titleViewHolder.setData("新番连载", "所有连载");
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null)
                            mListener.onTitleClick(v, 0);
                    }
                });
                break;
            case TYPE_LAST_TITLE: // 上一季度连载标题
                titleViewHolder = (DramaTitleViewHolder) holder;
                titleViewHolder.setData("7月新番", "分享列表");
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null)
                            mListener.onTitleClick(v, 1);
                    }
                });
                break;
            case TYPE_RECOMMEND_TITLE: // 推荐番剧标题
                titleViewHolder = (DramaTitleViewHolder) holder;
                titleViewHolder.itemView.setClickable(false);
                titleViewHolder.setData("番剧推荐", null);
                // 当不存在番剧推荐的数据列表时,隐藏标题
                if (mDramaTotalItemBean.getDramaRecommendItemList() == null) {
                    titleViewHolder.itemView.setVisibility(View.INVISIBLE);
                }
                break;
            case TYPE_NEW_CONTENT: // 新番连载内容
                SerializingViewHolder serializingViewHolder = (SerializingViewHolder) holder;
                // 设置新番连载内容的数据, 从当前位置减3是因为前面有轮播、三个按钮、新番连载标题
                final int pos = position - 3;
                serializingViewHolder.setData(mDramaTotalItemBean.getNewSerializingItemList().get
                        (pos));
                serializingViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onDramaClick(v, pos);
                        }
                    }
                });
                break;
            case TYPE_LAST_CONTENT:  // 上一季度连载内容
                serializingViewHolder = (SerializingViewHolder) holder;
                // 设置新番连载内容的数据, 从当前位置减3是因为前面有轮播、三个按钮、新番连载标题。
                // 减7是因为前面有6项新番内容和上一季度的新番标题
                final int pos1 = position - 3 - 7;
                serializingViewHolder.setData(mDramaTotalItemBean.getLastSerializingItemList()
                        .get(pos1));
                serializingViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onDramaClick(v, pos1);
                        }
                    }
                });
                break;
            case TYPE_RECOMMEND_CONTENT: // 推荐番剧内容
                DramaRecommendViewHolder recommendViewHolder = (DramaRecommendViewHolder) holder;
                // 设置新番连载内容的数据, 从当前位置减3是因为前面有轮播、三个按钮、新番连载标题。
                // 减7是因为前面有6项新番内容和上一季度的新番标题
                // 减4是因为前面有3项上一季度的新番内容和番剧推荐标题
                final int pos2 = position - 3 - 7 - 4;
                recommendViewHolder.setData(mDramaTotalItemBean.getDramaRecommendItemList().get
                        (pos2));
                recommendViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onRecommendClick(v, pos2);
                        }
                    }
                });
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        //LogUtil.d("我是视图类型" + position + " " + getItemCount());
        if (position == 0) { // 轮播
            return TYPE_BANNER;
        } else if (position == 1) { // 三个按钮
            // LogUtil.d("三个按钮类型");
            return TYPE_THREE_BUTTON;
        } else if (position == 2) { // 新番标题
            return TYPE_NEW_TITLE;
        } else if (position >= 3 && position <= 8) { // 新番内容
            return TYPE_NEW_CONTENT;
        } else if (position == 9) { // 上一季度新番标题
            return TYPE_LAST_TITLE;
        } else if (position >= 10 && position <= 12) { // 上一季度新番内容
            return TYPE_LAST_CONTENT;
        } else if (position == 13) { // 推荐新番标题
            return TYPE_RECOMMEND_TITLE;
        } else { // 推荐新番内容
            return TYPE_RECOMMEND_CONTENT;
        }
    }

    @Override
    public int getItemCount() {
        if (mDramaTotalItemBean == null) {
            return 0;
        } else {
            // 加上4分别是按钮项和3个标题项
            return mDramaTotalItemBean.getCount() + 4;
//            return 20;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        // 获取回收列表控件的布局管理器
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager != null && layoutManager instanceof GridLayoutManager) {
            // 转换成格子布局管理器
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            // 设置各类型格子的数量
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager
                    .SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type) {
                        // 轮播, 三个按钮, 新番标题, 上一季度新番标题, 番剧推荐标题, 番剧推荐内容都是占满
                        case TYPE_BANNER:
                        case TYPE_THREE_BUTTON:
                        case TYPE_NEW_TITLE:
                        case TYPE_LAST_TITLE:
                        case TYPE_RECOMMEND_TITLE:
                        case TYPE_RECOMMEND_CONTENT:
                            return gridLayoutManager.getSpanCount();
                        // 新番内容, 上一季度新番内容只占一格子
                        case TYPE_NEW_CONTENT:
                        case TYPE_LAST_CONTENT:
                            return 1;
                        default:
                            return 1;
                    }
                }
            });

            // 添加需要边距的项
            List<Integer> typeList = new ArrayList<>();
            typeList.add(TYPE_NEW_CONTENT);
            typeList.add(TYPE_LAST_CONTENT);
            typeList.add(TYPE_RECOMMEND_CONTENT);
            recyclerView.addItemDecoration(new GridMarginDecoration(context, typeList, 10));
        }
    }

    public DramaTotalItemBean getDramaTotalItemBean() {
        return mDramaTotalItemBean;
    }

    public void setDramaTotalItemBean(DramaTotalItemBean dramaTotalItemBean) {
        this.mDramaTotalItemBean = dramaTotalItemBean;
        notifyDataSetChanged();
    }

    public void addDramaRecommendList(List<DramaRecommendItemBean> dramaRecommendItemBeanList) {
        mDramaTotalItemBean.getDramaRecommendItemList().addAll(dramaRecommendItemBeanList);
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mListener = onItemClickListener;
    }

    public OnItemClickListener getOnItemClickListener() {
        return mListener;
    }


    /**
     * 列表项点击事件
     */
    public interface OnItemClickListener {

        /**
         * 轮播点击事件
         *
         * @param sliderLayout       控件
         * @param bannerItemBeanList 数据
         */
        void onBannerClick(SliderLayout sliderLayout, List<BannerItemBean> bannerItemBeanList);

        /**
         * 三个按钮点击事件
         *
         * @param view 视图
         * @param what 第几个
         */
        void onThreeBtnClick(View view, int what);

        /**
         * 标题点击事件
         *
         * @param view 视图
         * @param what 第几个
         */
        void onTitleClick(View view, int what);

        /**
         * 番剧点击事件
         *
         * @param view 视图
         * @param pos  位置
         */
        void onDramaClick(View view, int pos);

        /**
         * 推荐点击事件
         *
         * @param view 视图
         * @param pos  位置
         */
        void onRecommendClick(View view, int pos);

    }

}
